package com.voja.salonschedule;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.voja.salonschedule.ads.MyAdListener;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.LinearLayout;

public class BaseChildActivity extends ActionBarActivity{
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void initAd(int viewId) {
        LinearLayout adContainer = (LinearLayout)findViewById(viewId);
        Resources res            = getResources();
        if (adContainer.equals(null)) {
            return;
        }
        
        // AdView
        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(res.getString(R.string.admob_id));
        
        // add to container
        adContainer.addView(adView);
        
        // AdListener
        MyAdListener adListener = new MyAdListener(this, adView);
        adView.setAdListener(adListener);
        
        // AdRequest
        AdRequest.Builder builder = new AdRequest.Builder();
        
        String[] keywords = res.getStringArray(R.array.ad_keywords);
        for (int i=0; i<keywords.length; i++) {
            builder.addKeyword(keywords[i]);
        }
        builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        AdRequest adRequest = builder.build();
        adView.loadAd(adRequest);
    }
    
    protected String getExtra(String key) {
    	return getExtra(key, getIntent());
    }
    protected String getExtra(String key, Intent intent) {
        String value = null;
        Bundle extras = intent.getExtras();
        if (! extras.equals(null)) {
            value = extras.getString(key);
        }
        
        return value;
    }
}
